var Fly = require("flyio/dist/npm/wx");
var qs = require('qs');
var fly = new Fly;

fly.config.timeout = 15000;
fly.config.baseURL = 'https://www.easy-mock.com/mock/5b236d94b541690d6ed33acf/lupanzi/api/';
//fly.config.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
fly.config.headers['Content-Type'] = 'application/x-www-form-urlencoded';

// Add a request interceptor
fly.interceptors.request.use(
	(request)=>{
	    // Do something before request is sent
	    // Complete the request with custom data
	    // return Promise.resolve("fake data")
	},(err) => {
	    // Do something with response error
	    return Promise.resolve("err")
	}
)

// Add a response interceptor
fly.interceptors.response.use(
    (response) => {
      // Do something with response data .
      // Just return the data field of response
      return response.data
    },
    (err) => {
      // Do something with response error
      return Promise.resolve("err")
    }
)

//fly
function requestEntrance(data, callback) {
    if (typeof(callback) === 'object') {
        callback.success = callback.success || function() {};
        callback.error = callback.error || function() {};
        callback.complete = callback.complete || function() {};
        //fly.request(data.url,qs.stringify(data.data),{method:data.method})
        fly.request(data.url,data.data,{method:data.method})
            .then(function(response) {
                if (response) {
                    callback.complete();
                    callback.success(response.data);
                }
            })
            .catch(function(error) {
                callback.complete();
                callback.error(error);
                alert('Oops! There was a problem connecting to the network, on this (' + data.url + ')');
                console.log('error:', data.url, error);
            });
    }
}

/****************************** api **************************/
//API test
export function test(obj) {
    let requestData = {
            method: 'get',
            url: 'test',
            data: obj.data
        },
        requestCallback = {};
    requestCallback.success = function(response) {
        obj.success ? obj.success(response) : '';
    };
    requestEntrance(requestData, requestCallback);
}

//商户列表
export function merchantList(obj) {
    let requestData = {
            method: 'get',
            url: 'merchantList'
        },
        requestCallback = {};
    requestCallback.success = function(response) {
        obj.success ? obj.success(response) : '';
    };
    requestEntrance(requestData, requestCallback);
}
